﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpaceTrader
{

    public partial class Form1 : Form
    {
        const int maxitems = 5;
        const int maxplanets = 5;

        public static int turn = 0;
        public static Random rnd1 = new Random();
        public static int index;

        public struct commodity
        {
            public string name;
            public int baseprice;
            public int basequant;
            public int rarity;

            public commodity(string n, int bp, int bq, int r)
            {
                name = n;
                baseprice = bp;
                basequant = bq;
                rarity = r;
            }
        }

        public struct localmarket
        {
            public int price;
            public int quantity;
        }

        public class entity
        {
            public string name;
            public localmarket[] store = new localmarket[maxitems];
        }

        public class person : entity
        {
            public int cash;
            public int location;
            public int holdspace;
            public int holdext;

            public person(string n, int c, int l, int hs)
            {
                name = n;
                cash = c;
                location = l;
                holdspace = hs;
                holdext = 0;
            }

        }

        public class place : entity
        {
            public int govtype;
            public int indtype;

            public place(string n, int g, int i)
            {
                name = n;
                govtype = g;
                indtype = i;
            }

            public void GenMarket(int m, commodity[] t)
            {
                for (int i = 0; i < m; i++)
                {
                    this.store[i].price = t[i].baseprice + rnd1.Next((0 - t[i].rarity), t[i].rarity);
                    if (this.store[i].price < 1)
                    {
                        this.store[i].price = t[i].baseprice;
                    }
                    this.store[i].quantity = t[i].basequant + rnd1.Next((0 - t[i].rarity), t[i].rarity);
                    if (this.store[i].quantity < 1)
                    {
                        this.store[i].quantity = 0;
                    }
                }

            }

        }

        commodity[] tradegoods = {
                                         new commodity("Water               ", 40, 20, 5),
                                         new commodity("Food                ", 30, 15, 3),
                                         new commodity("Fuel                ", 50, 28, 10),
                                         new commodity("Carbon Ice          ", 2000, 4, 20),
                                         new commodity("Luxuries            ", 4000, 5, 50)
                                     };

        place[] planet = {
                                 new place("Earth", 1, 1),
                                 new place("Mars", 2, 3),
                                 new place("Saturn", 3, 2),
                                 new place("Jupiter", 2, 1),
                                 new place("Europa",2,1)
                             };

        person player = new person("Justin", 100, 0, 20);
      
        public Form1()
        {
            InitializeComponent();

            labelPlayerName.Text = "Name: " + player.name;
            labelPlayerCash.Text = "Cash: " + player.cash;
            labelPlayerHold.Text = "Holdspace: " + player.holdspace + "t";
            label1.Text = "Planet: " + planet[player.location].name;
            label2.Text = "Hold capacity: " + (20 + (player.holdext * 10)) + "t";

            tabControl1.TabPages[0].Text = "Trade Items (" + planet[player.location].name + ")";
            tabControl1.TabPages[1].Text = "Ship";

            // Fill the combobox with list of available planets
            for (int i = 0;i < maxplanets;i++)
            {
                comboBox1.Items.Add(planet[i].name);
            }
            // Set the selected planet to where the player starts
            comboBox1.SelectedIndex = player.location;
            
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            index = listView1.Items.IndexOf(listView1.SelectedItems[0]);

            switch (e.Button)
            {
                case MouseButtons.Left:
                    if (player.cash > 0 & planet[player.location].store[index].quantity > 0 & player.holdspace > 0 & planet[player.location].store[index].price < player.cash)
                    {
                        player.cash -= planet[player.location].store[index].price;
                        player.holdspace--;
                        player.store[index].quantity++;
                        planet[player.location].store[index].quantity--;
                    }
                    break;
                case MouseButtons.Right:
                    if (player.store[index].quantity > 0)
                    {
                        player.cash += planet[player.location].store[index].price;
                        player.holdspace++;
                        player.store[index].quantity--;
                        planet[player.location].store[index].quantity++;
                    }
                    break;
            }

            labelPlayerCash.Text = "Cash: " + player.cash;
            labelPlayerHold.Text = "Holdspace: " + player.holdspace + "t";

            listView1.BeginUpdate();
            listView1.Items.Clear();
            for (int i = 0; i < tradegoods.Length; i++)
            {
                ListViewItem listitem = new ListViewItem(tradegoods[i].name);
                listitem.SubItems.Add(Convert.ToString(planet[player.location].store[i].price));
                listitem.SubItems.Add(Convert.ToString(planet[player.location].store[i].quantity));
                listitem.SubItems.Add(Convert.ToString(player.store[i].quantity));
                listView1.Items.Add(listitem);
            }
            listView1.EndUpdate();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show( Convert.ToString(comboBox1.SelectedIndex));
            // If the player has selected a different planet then travel to it and generate
            // a new planet market
            if ((turn == 0) || (player.location != comboBox1.SelectedIndex))
            {
                player.location = comboBox1.SelectedIndex;
                // Generate the current systems market
                planet[player.location].GenMarket(maxitems, tradegoods);

                // Fill the ListView with the current planets market
                listView1.Items.Clear();
                for (int i = 0; i < tradegoods.Length; i++)
                {
                    ListViewItem listitem = new ListViewItem(tradegoods[i].name);
                    listitem.SubItems.Add(Convert.ToString(planet[player.location].store[i].price));
                    listitem.SubItems.Add(Convert.ToString(planet[player.location].store[i].quantity));
                    listitem.SubItems.Add(Convert.ToString(player.store[i].quantity));
                    listView1.Items.Add(listitem);
                }
                listView1.EndUpdate();

                label1.Text = "Planet: " + planet[player.location].name;
                turn++;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (player.cash > 1000)
            {
                player.cash -= 1000;
                player.holdext++;
                player.holdspace +=  10;

                labelPlayerCash.Text = "Cash: " + player.cash;
                labelPlayerHold.Text = "Hold: " + player.holdspace;
                label2.Text = "Hold capacity: " + (20 + (player.holdext * 10)) + "t";

            }

        }

    }
}
